home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / configglue / parsers.py < prev    next >
Text File  |  2009-06-15  |  1KB  |  43 lines

  1. # This file is part of configglue, by John R. Lenton <john.lenton@canonical.com>
  2. # (C) 2009 by Canonical Ltd.
  3. # Released under the BSD License (see the file LICENSE)
  4. # For bug reports, support, and new releases: http://launchpad.net/configglue
  5.  
  6. """Parsers used by TypedConfigParser live here
  7. """
  8.  
  9. def lines(value):
  10.     """ Split a string on its newlines
  11.  
  12.     RawConfigParser supports "continuations in the style of RFC822", which
  13.     gives us a very natural way of having values that are lists of strings.
  14.  
  15.     If value isn't a string, leaves it alone.
  16.     """
  17.     try:
  18.         return value.split('\n')
  19.     except AttributeError:
  20.         return value
  21.  
  22. _true_values = frozenset(('true', '1', 'on', 'yes'))
  23. _false_values = frozenset(('false', '0', 'off', 'no'))
  24. def bool_parser(value):
  25.     """Take a string representation of a boolean and return its boolosity
  26.  
  27.     true, 1, on, and yes (in any case) should all be True.
  28.     false, 0, off, and no (in any case) should all be False.
  29.  
  30.     any other string else should raise an error; None and booleans are
  31.     preserved.
  32.     """
  33.     try:
  34.         value = value.lower()
  35.     except AttributeError:
  36.         return bool(value)
  37.     else:
  38.         if value in _true_values:
  39.             return True
  40.         if value in _false_values:
  41.             return False
  42.     raise ValueError("Unable to determine boolosity of %r" % value)
  43.